{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Install d4rl-atari."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install git+https://github.com/takuseno/d4rl-atari"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setup dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gym\n",
    "import d4rl_atari\n",
    "\n",
    "# setup environment including dataset\n",
    "# caution: this dataset will consume around 8GiB of CPU memory!!\n",
    "env = gym.make('breakout-expert-v0')\n",
    "\n",
    "# extract dataset\n",
    "data = env.get_dataset()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setup data-driven deep reinforcement learning."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from d3rlpy.dataset import MDPDataset\n",
    "from d3rlpy.algos import DiscreteCQL\n",
    "from d3rlpy.metrics.scorer import discounted_sum_of_advantage_scorer\n",
    "from d3rlpy.metrics.scorer import evaluate_on_environment\n",
    "from d3rlpy.metrics.scorer import td_error_scorer\n",
    "from d3rlpy.metrics.scorer import average_value_estimation_scorer\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# setup MDPDataset\n",
    "dataset = MDPDataset(data['observations'],\n",
    "                     data['actions'],\n",
    "                     data['rewards'],\n",
    "                     data['terminals'],\n",
    "                     discrete_action=True) # this flag is necessary!\n",
    "\n",
    "# this can be replaced with the builtin helper funciton as follows:\n",
    "# from d3rlpy.datasets import get_atari\n",
    "# dataset, env = get_atari('breakout-expert-v0')\n",
    "\n",
    "# setup CQL algorithm (discrete version)\n",
    "cql = DiscreteCQL(n_frames=4, scaler='pixel', use_gpu=True)\n",
    "\n",
    "# split train and test episodes\n",
    "train_episodes, test_episodes = train_test_split(dataset, test_size=0.2)\n",
    "\n",
    "# start training\n",
    "cql.fit(train_episodes,\n",
    "        eval_episodes=test_episodes,\n",
    "        n_epochs=10,\n",
    "        scorers={\n",
    "            'environment': evaluate_on_environment(env), # use d4rl-atari environment\n",
    "            'advantage': discounted_sum_of_advantage_scorer, # smaller is better\n",
    "            'td_error': td_error_scorer, # smaller is better\n",
    "            'value_scale': average_value_estimation_scorer # smaller is better\n",
    "        })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
